package org.fall.view.controller;

import org.fall.view.model.ResultEntity;
import org.fall.view.model.dto.MailBoxDTO;
import org.fall.view.model.vo.MailBoxVO;
import org.fall.view.model.vo.MemberUserVO;
import org.fall.view.service.MemberUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 会员用户-工作之余标签-控制器
 * @author sprogFall
 * @since 2021/4/13 15:56
 */
@Controller
@RequestMapping("member/life")
public class MemberUserLifeController {

    @Resource
    private MemberUserService memberUserService;

    /**
     * 跳转到mailBox页面
     * @param modelMap modelMap
     * @param session session
     * @return 跳转页面
     */
    @GetMapping("mailBox/page")
    public String getMailBoxPage(ModelMap modelMap, HttpSession session) {
        // 收件人list
        List<MemberUserVO> list = memberUserService.getMemberUserVOList();
        modelMap.put("userList", list);
        // 读取草稿
        Integer loginId = (Integer) session.getAttribute("loginId");
        MailBoxVO mailBox = memberUserService.getSavedMail(loginId);
        modelMap.put("savedMail", mailBox);
        return "pages/life/mailBox";
    }

    /**
     * 跳转到收件箱页面
     * @param type 邮件种类
     * @param anonymous 是否匿名
     * @param modelMap modelMap
     * @param session session
     * @return 跳转收件箱页面
     */
    @GetMapping("mailBox/get")
    public String showAcceptMailBox(@RequestParam(value = "type", defaultValue = "") String type,
                              @RequestParam(value = "anonymous", defaultValue = "") String anonymous,
            ModelMap modelMap, HttpSession session) {
        Integer loginId = (Integer) session.getAttribute("loginId");
        List<MailBoxVO> list = memberUserService.getAcceptMail(loginId, type, anonymous);
        modelMap.put("list", list);
        // 当前为收件箱
        modelMap.put("type", "accept");
        return "pages/life/mailBoxShow";
    }

    /**
     * 跳转到发件箱页面
     * @param type 邮件种类
     * @param modelMap modelMap
     * @param session session
     * @return 跳转发件箱页面
     */
    @GetMapping("mailBox/get/send")
    public String showSendMailBox(@RequestParam(value = "type", defaultValue = "") String type,
                              ModelMap modelMap, HttpSession session) {
        Integer loginId = (Integer) session.getAttribute("loginId");
        List<MailBoxVO> list = memberUserService.getSendMail(loginId, type);
        modelMap.put("list", list);
        // 当前为发件箱
        modelMap.put("type", "send");
        return "pages/life/mailBoxShow";
    }

    /**
     * 发送邮件
     * @param dto dto
     * @param session session
     * @return 发送结果
     */
    @PostMapping("mailBox/send")
    @ResponseBody
    public ResultEntity sendMail(@RequestBody MailBoxDTO dto, HttpSession session) {
        Integer loginUserId = (Integer) session.getAttribute("loginId");
        return memberUserService.sendMail(dto, loginUserId);
    }

    /**
     * 保存草稿
     * @param dto dto
     * @param session session
     * @return 保存结果
     */
    @PostMapping("mailBox/save")
    @ResponseBody
    public ResultEntity saveMail(@RequestBody MailBoxDTO dto, HttpSession session) {
        Integer loginUserId = (Integer) session.getAttribute("loginId");
        return memberUserService.saveMail(dto, loginUserId);
    }
}
